package logic

type FlowerType int

const (
	//方块
	DIAMOND FlowerType = iota
	//梅花
	PLUM
	//红桃
	HEART
	//黑桃
	SPADE
)
type CardPoint int
const (
	C3 CardPoint = iota + 3
	C4
	C5
	C6
	C7
	C8
	C9
	C10
	CJ
	CQ
	CK
	CA
	C2
	CBlackJoker
	CRedJoker
)

type Card struct {
	//牌面点数，
	//3 4 5 6 7 8 9 10 J  Q  K  A  2 小王 大王 ->
	//3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
	Number int
	//扑克花色，当牌为大王或小王时该值无意义
	Flower FlowerType
}
func (this *Card) Value() int {
	return this.Number
}
func (this *Card) String() string {
	return "{" + string(this.Number) + "," + string(this.Flower) + "}"
}
func (a Card) IsEuqal(b Card) bool {
	return a.Number == b.Number && a.Flower == b.Flower
}

func (a Card) IsValueEuqal(b Card) bool {
	return a.Number == b.Number
}

type CardSlice []Card

//实现sort排序接口的Len,Swap,Less方法
// 重写 Len() 方法
func (a CardSlice) Len() int {
	return len(a)
}

// 重写 Swap() 方法
func (a CardSlice) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

// 重写 Less() 方法， 数值从小到大排序，数值相等花色由小到大
func (a CardSlice) Less(i, j int) bool {
	if a[i].Number != a[j].Number {
		return a[i].Number < a[j].Number
	}
	return int(a[i].Flower) < int(a[j].Flower)
}

func Sort111(c []Card, cardsType CardsType) []Card {
	var res []Card
	switch cardsType {
	case THREEANDSINGLE:
		if c[0].Number != c[1].Number {
			res = make([]Card, len(c))
			copy(res, c)
			res[0], res[3] = res[3], res[0]
		}
	case THREEANDDOUBLE:
		if c[0].Number != c[2].Number {
			res = make([]Card, 0)
			res = append(res, c[2:5]...)
			res = append(res, c[0:2]...)
		}
	case AIRPLANEANDSINGLE:
		fallthrough
	case AIRPLANEANDDOUBLE:
		threeIdx := 0
		for i, _ := range c {
			if c[i].Number == c[i+1].Number && c[i+1].Number == c[i+2].Number {
				threeIdx = i
				break
			}
		}
		threeEndIdx := 0
		for i := threeIdx; i < len(c)-2; {
			if c[i].Number == c[i+1].Number && c[i+1].Number == c[i+2].Number {
				i += 3
				threeEndIdx = i
				continue
			} else {
				break
			}
		}

		res = make([]Card, 0)
		res = append(res, c[threeIdx:threeEndIdx]...)
		if threeIdx == 0 {
			res = append(res, c[threeEndIdx:]...)
		} else if threeIdx > 0 && threeEndIdx < len(c) {
			res = append(res, c[0:threeIdx]...)
			res = append(res, c[threeEndIdx:]...)
		} else if threeIdx > 0 && threeEndIdx == len(c) {
			res = append(res, c[0:threeIdx]...)
		}
	case FOURANDSINGLETWO:
		fallthrough
	case FOURANDDOUBLETWO:
		fourIdx := 0
		for i, _ := range c {
			if c[i].Number == c[i+1].Number && c[i+1].Number == c[i+2].Number && c[i+2].Number == c[i+3].Number {
				fourIdx = i
				break
			}
		}
		res = make([]Card, 0)
		res = append(res, c[fourIdx:fourIdx+4]...)
		if fourIdx == 0 {
			res = append(res, c[fourIdx+4:]...)
		} else if fourIdx > 0 && fourIdx < len(c) {
			res = append(res, c[:fourIdx]...)
			res = append(res, c[fourIdx+4:]...)
		} else {
			res = append(res, c[:fourIdx]...)
		}

	default:

		return c
	}

	return res
}
type CardsType int

const (
	//无类型 0
	NONE CardsType = iota
	//单张 	1
	SINGLE
	//对子 	2
	DOUBLE
	//三张 	3
	THREE
	//三带一 4
	THREEANDSINGLE
	//三带二 5
	THREEANDDOUBLE
	//单顺子 6
	SINGLESTRAIGHT
	//双顺子 7
	DOUBLESTRAIGHT
	//三顺子 8
	THREESTRAIGHT
	//飞机带单张 9
	AIRPLANEANDSINGLE
	//飞机带对子 10
	AIRPLANEANDDOUBLE
	//四带二单 11
	FOURANDSINGLETWO
	//四带二对 12
	FOURANDDOUBLETWO
	//炸弹 	13
	BOMB
	//火箭 	14
	ROCKET
)

//取较小的整数
func Min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//取较大的整数
func Max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

//取较小的整数
func MinInt64(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

//取较大的整数
func MaxInt64(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}
